package com.sugar.reflection;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 *
 * 重点：反射结合配置文件
 * @author felix
 * @date 2025/2/10 17:15
 */
public class ReflectDemo9 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Properties properties = new Properties();
//        FileInputStream fileInputStream = new FileInputStream("prop.properties");
        InputStream inputStream = ReflectDemo9.class.getClassLoader().getResourceAsStream("prop.properties");
        properties.load(inputStream);
        if (inputStream != null) {
            inputStream.close();
        }
        System.out.println(properties);
        String classname = properties.get("classname") + "";
        String methodName = properties.get("methodname") + "";
        System.out.println(classname);
        System.out.println(methodName);

        Class<?> clazz = Class.forName(classname);
        Constructor<?> con = clazz.getDeclaredConstructor();
        con.setAccessible(true);
        Object o = con.newInstance();
        System.out.println(o);

        Method method = clazz.getMethod(methodName);
        method.invoke(o);

    }
}
